﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:ScheduleMVVM.ViewModel"
                                   x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
  
  OR (WPF only):
  
  xmlns:vm="clr-namespace:ScheduleMVVM.ViewModel"
  DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
*/

namespace ScheduleMVVM.ViewModel
{
    /// <summary>
    /// This class contains static references to all the view models in the
    /// application and provides an entry point for the bindings.
    /// <para>
    /// Use the <strong>mvvmlocatorproperty</strong> snippet to add ViewModels
    /// to this locator.
    /// </para>
    /// <para>
    /// In Silverlight and WPF, place the ViewModelLocatorTemplate in the App.xaml resources:
    /// </para>
    /// <code>
    /// &lt;Application.Resources&gt;
    ///     &lt;vm:ViewModelLocatorTemplate xmlns:vm="clr-namespace:ScheduleMVVM.ViewModel"
    ///                                  x:Key="Locator" /&gt;
    /// &lt;/Application.Resources&gt;
    /// </code>
    /// <para>
    /// Then use:
    /// </para>
    /// <code>
    /// DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
    /// </code>
    /// <para>
    /// You can also use Blend to do all this with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// <para>
    /// In <strong>*WPF only*</strong> (and if databinding in Blend is not relevant), you can delete
    /// the Main property and bind to the ViewModelNameStatic property instead:
    /// </para>
    /// <code>
    /// xmlns:vm="clr-namespace:ScheduleMVVM.ViewModel"
    /// DataContext="{Binding Source={x:Static vm:ViewModelLocatorTemplate.ViewModelNameStatic}}"
    /// </code>
    /// </summary>
    public class ViewModelLocator
    {
        /// <summary>
        /// Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            ClearMain();
            ClearSearch();
            ClearSettings();
            ClearRoute();
            ClearFavourites();
            ClearAddFavourite();
        }
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view models
            ////}
            ////else
            ////{
            ////    // Create run time view models
            ////}

            CreateMain();            
        }
        public static void PostInitialization()
        {
            CreateSearch();
            CreateSettings();
            CreateRoute();
            CreateFavourites();
            CreateAddFavourite();
        }
        #region MainViewModel
        private static MainViewModel _main;

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        public static MainViewModel MainStatic
        {
            get
            {
                if (_main == null)
                {
                    CreateMain();
                }

                return _main;
            }
        }

        /// <summary>
        /// Gets the Main property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public MainViewModel Main
        {
            get
            {
                return MainStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Main property.
        /// </summary>
        public static void ClearMain()
        {
            if (_main != null)
            {
                _main.Cleanup();
                _main = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Main property.
        /// </summary>
        public static void CreateMain()
        {
            if (_main == null)
            {
                _main = new MainViewModel();
            }
        }

        #endregion
        #region SearchViewModel
        private static SearchViewModel _search;

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        public static SearchViewModel SearchStatic
        {
            get
            {
                if (_search == null)
                {
                    CreateSearch();
                }

                return _search;
            }
        }

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SearchViewModel Search
        {
            get
            {
                return SearchStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Settings property.
        /// </summary>
        public static void ClearSearch()
        {
            if (_search != null)
            {
                _search.Cleanup();
                _search = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Settings property.
        /// </summary>
        public static void CreateSearch()
        {
            if (_search == null)
            {
                _search = new SearchViewModel();
            }
        }
        #endregion
        #region SettingsViewModel
        private static SettingsViewModel _settings;

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        public static SettingsViewModel SettingsStatic
        {
            get
            {
                if (_settings == null)
                {
                    CreateSettings();
                }

                return _settings;
            }
        }

        /// <summary>
        /// Gets the Settings property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public SettingsViewModel Settings
        {
            get
            {
                return SettingsStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Settings property.
        /// </summary>
        public static void ClearSettings()
        {
            if (_settings != null)
            {
                _settings.Cleanup();
                _settings = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Settings property.
        /// </summary>
        public static void CreateSettings()
        {
            if (_settings == null)
            {
                _settings = new SettingsViewModel();
            }
        }
        #endregion
        #region RouteViewModel
        private static RouteViewModel _route;

        /// <summary>
        /// Gets the Route property.
        /// </summary>
        public static RouteViewModel RouteStatic
        {
            get
            {
                if (_route == null)
                {
                    CreateRoute();
                }

                return _route;
            }
        }

        /// <summary>
        /// Gets the Route property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public RouteViewModel Route
        {
            get
            {
                return RouteStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Route property.
        /// </summary>
        public static void ClearRoute()
        {
            if (_route != null)
            {
                _route.Cleanup();
                _route = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Route property.
        /// </summary>
        public static void CreateRoute()
        {
            if (_route == null)
            {
                _route = new RouteViewModel();
            }
        }
        #endregion
        #region FavouritesViewModel
        private static FavouritesViewModel _favourites;

        /// <summary>
        /// Gets the Favourites property.
        /// </summary>
        public static FavouritesViewModel FavouritesStatic
        {
            get
            {
                if (_favourites == null)
                {
                    CreateFavourites();
                }

                return _favourites;
            }
        }

        /// <summary>
        /// Gets the Favourites property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public FavouritesViewModel Favourites
        {
            get
            {
                return FavouritesStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the Favourites property.
        /// </summary>
        public static void ClearFavourites()
        {
            if (_favourites != null)
            {
                _favourites.Cleanup();
                _favourites = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the Favourites property.
        /// </summary>
        public static void CreateFavourites()
        {
            if (_favourites == null)
            {
                _favourites = new FavouritesViewModel();
            }
        }
        #endregion
        #region AddFavouriteViewModel
        private static AddFavouriteViewModel _addfav;

        /// <summary>
        /// Gets the AddFavourite property.
        /// </summary>
        public static AddFavouriteViewModel AddFavouriteStatic
        {
            get
            {
                if (_addfav == null)
                {
                    CreateAddFavourite();
                }

                return _addfav;
            }
        }

        /// <summary>
        /// Gets the AddFavourite property.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
            "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AddFavouriteViewModel AddFavourite
        {
            get
            {
                return AddFavouriteStatic;
            }
        }

        /// <summary>
        /// Provides a deterministic way to delete the AddFavourite property.
        /// </summary>
        public static void ClearAddFavourite()
        {
            if (_addfav != null)
            {
                _addfav.Cleanup();
                _addfav = null;
            }
        }

        /// <summary>
        /// Provides a deterministic way to create the AddFavourite property.
        /// </summary>
        public static void CreateAddFavourite()
        {
            if (_addfav == null)
            {
                _addfav = new AddFavouriteViewModel();
            }
        }
        #endregion
    }
}